package in.muton.edushare.appsettings;

import in.muton.edushare.EduShareSession;
import in.muton.edushare.url.URLLoader;

import org.apache.wicket.RestartResponseException;
import org.apache.wicket.markup.html.WebPage;
import org.apache.wicket.markup.html.form.Form;
import org.apache.wicket.markup.html.form.PasswordTextField;
import org.apache.wicket.markup.html.form.TextArea;
import org.apache.wicket.markup.html.form.TextField;
import org.apache.wicket.model.PropertyModel;

import com.edushare.common.constants.ConfigPropertyName;
import com.edushare.common.constants.URLConstants;
import com.edushare.common.dao.ConfigPropertiesDAO;
import com.edushare.common.global.SystemGlobals;
import com.edushare.common.mailer.SimpleMailer;
import com.edushare.common.orm.ORMInterface;
import com.edushare.common.vo.ConfigProperties;
import com.edushare.common.vo.UserRoles;
import com.edushare.common.vo.Users;

/**
 * This page loads up when user installs the system for the form time
 * or *A UNIVERSITY ADMIN* accesses <base_url>/app-config
 * @author naishe
 *
 */
public class ConfigApp extends WebPage {
	
	private ConfigProperties uploadDirProp;
	private ConfigProperties uploadLtProp;
	private ConfigProperties smtpHostProp;
	private ConfigProperties smtpPortProp;
	private ConfigProperties mailIdProp;
	private ConfigProperties mailPasswordProp;
	
	public String testEmailId = "";
	public String testContent = "";
	
	public ConfigApp(){
		EduShareSession.get().bind();
		
		//IF this is not the first boot
		if(
				!SystemGlobals
					.getInstance()
					.getConfigProperty(ConfigPropertyName.SUPPORT_BOOT)
					.equalsIgnoreCase("true")
					
		){
			//if user is not logged in, ask him to
			if(!EduShareSession.get().isLoggedIn())
				throw new RestartResponseException(URLLoader.getPageClass(URLConstants.LOGIN_PATH));
			else //if logged-in check if he is Admin of any university
			{
				Users user = EduShareSession.get().getLoggedInUser();
				boolean isAdmin = false;
				for(UserRoles role: user.getRoleList()){
					if(role.getRoleType() == UserRoles.ADMIN_ROLE){
						isAdmin = true;
						break;
					}
				}
				if(!isAdmin)
					throw new RestartResponseException(URLLoader.getPageClass(URLConstants.UNAUTHORIZED_PATH));
			}
			
		}
		Form<Void> form = new Form<Void>("form"){

			private static final long serialVersionUID = 6327572349414633473L;

			@Override
			protected void onSubmit() {
				SystemGlobals.getInstance().resetSystemGlobals();
				getSessionORMInterface().commitAllChanges();
			}
		};
		this.uploadDirProp = new ConfigPropertiesDAO(getSessionORMInterface()).getConfigProperty(ConfigPropertyName.STORAGE_ROOT_DIRECTORY);
		this.uploadLtProp = new ConfigPropertiesDAO(getSessionORMInterface()).getConfigProperty(ConfigPropertyName.UPLOAD_MAXSIZE_KB);
		this.smtpHostProp = new ConfigPropertiesDAO(getSessionORMInterface()).getConfigProperty(ConfigPropertyName.MAIL_HOST);
		this.smtpPortProp = new ConfigPropertiesDAO(getSessionORMInterface()).getConfigProperty(ConfigPropertyName.MAIL_PORT);
		this.mailIdProp = new ConfigPropertiesDAO(getSessionORMInterface()).getConfigProperty(ConfigPropertyName.MAIL_NOREPLY_USER);
		this.mailPasswordProp = new ConfigPropertiesDAO(getSessionORMInterface()).getConfigProperty(ConfigPropertyName.MAIL_NOREPLY_PASSWORD);
		
		add(form);
		TextField<String> storageDir = 
			new TextField<String>("storageDir", new PropertyModel<String>(ConfigApp.this.uploadDirProp, "value"));
		form.add(storageDir);
		TextField<String> uploadLt = 
			new TextField<String>("uploadLt", new PropertyModel<String>(ConfigApp.this.uploadLtProp, "value"));
		form.add(uploadLt);
		TextField<String> smtpHost = 
			new TextField<String>("smtpHost", new PropertyModel<String>(ConfigApp.this.smtpHostProp, "value"));
		form.add(smtpHost);
		TextField<String> smtpPort = 
			new TextField<String>("smtpPort", new PropertyModel<String>(ConfigApp.this.smtpPortProp, "value"));
		form.add(smtpPort);
		TextField<String> mailId = 
			new TextField<String>("mailId", new PropertyModel<String>(ConfigApp.this.mailIdProp, "value"));
		form.add(mailId);
		PasswordTextField mailPassword = 
			new PasswordTextField("mailPassword", new PropertyModel<String>(ConfigApp.this.mailPasswordProp, "value"));
		mailPassword.setResetPassword(false);
		form.add(mailPassword);
		
		
		//test mail form
		Form<Void> testMailForm = new Form<Void>("testMailForm"){
			@Override
			protected void onSubmit() {
				SimpleMailer.sendMail(testEmailId, testContent);
			}
		};
		
		testMailForm.add(new TextField<String>("toId", new PropertyModel<String>(ConfigApp.this, "testEmailId")).setRequired(true));
		testMailForm.add(new TextArea<String>("testContent", new PropertyModel<String>(ConfigApp.this, "testContent")).setRequired(true));
		add(testMailForm);
		
		//Approve settings for
		Form<Void> approveForm = new Form<Void>("approveSettings"){
			@Override
			protected void onSubmit() {
				boolean redirectToNewUniv = false;
				if(SystemGlobals.getInstance().getConfigProperty(ConfigPropertyName.SUPPORT_BOOT).equalsIgnoreCase("true"))
					redirectToNewUniv = true;
				if(redirectToNewUniv){
//					throw new RestartResponseException(URLLoader.getPageClass(URLConstants.REGISTER_PATH));
					setResponsePage(URLLoader.getPageClass(URLConstants.REGISTER_PATH));
				}else{
//					throw new RestartResponseException(URLLoader.getPageClass(URLConstants.LOGIN_PATH));
					setResponsePage(URLLoader.getPageClass(URLConstants.LOGIN_PATH));
				}
			}
		};
		add(approveForm);
	}

	private ORMInterface getSessionORMInterface() {
		return EduShareSession.get().getSessionORMInterface();
	}
	
}
